/*表单验证 by hsq*/
var validateCfgErrorMsg=null;
function validate()
{
	var valid=true;
	$(':input[validateType][disabled!=true]').each(function(){
		if(!checkValue($(this))) {
			valid=false;
			if($(this).attr("validateGroupOr")!=""){
				$(":input[validateType][disabled!=true][validateGroupOr='"+$(this).attr("validateGroupOr")+"']").each(function(){
					if(checkValue($(this))){
						valid=true;
						return false;
					}
				});
			}
			if(valid==false){
				showMsg($(this));
				return false;
			}
		}
		if(valid==false) return false;
	});
	
	return valid;
}

function checkValue(obj)
{
	var validateType=obj.attr("validateType");
	var validateTypeArray=validateType.split(",");
	var value=obj.val();
	for(var i=0;i<validateTypeArray.length;i++)
	{
		validateType=validateTypeArray[i];
		if(validateType=="required")
		{
			if($.trim(value)=="") 
			{
				validateCfgErrorMsg="不能为空";
				return false;
			}
		}
		else if(validateType=="length")
		{
			//alert("length validate");
			var lengthValue=obj.attr("length");
			if(!lengthValue)
			{
				validateCfgErrorMsg="标签必须带length属性,否则无法验证";
				return false;
			}
			else
			{
				regex = new RegExp("^[0-9]*\,[0-9]*$","g");
				if(!regex.test(lengthValue))
				{
					validateCfgErrorMsg="标签带length属性格式如：\"20,30\"";
					return false;
				}else{
					var lengthValueArray = lengthValue.split(",");
					if(lengthValueArray[0] && lengthValueArray[1] && lengthValueArray[0]>lengthValueArray[1]){
						var lengthValueTmp = lengthValueArray[0];
						lengthValueArray[0] = lengthValueArray[1];
						lengthValueArray[1] = lengthValueTmp;
					}
					if(!lengthValueArray[0]){
						lengthValueArray[0] = "0";
					}
					var str = new String(value);
					if(lengthValueArray[0] && lengthValueArray[0] > getByte(str)){
						validateCfgErrorMsg="字符串长度过短，不能少于" + lengthValueArray[0] + "个字符(一个中文为两个字符)";
						//validateCfgErrorMsg="字符串长度过短，不能少于" + lengthValueArray[0] + "个字符" + lengthValueArray[1] + "+" + value.length;
						return false;
					}else if(lengthValueArray[1] && lengthValueArray[1] < getByte(str)){
						validateCfgErrorMsg="字符串长度过长，不能多于" + lengthValueArray[1] + "个字符(一个中文为两个字符)";
						return false;
					}
				}
			}
		}
		else if(validateType=="regex")
		{
			var regex=obj.attr("regex");
			if(!regex)
			{
				validateCfgErrorMsg="标签必须带regex属性,否则无法验证";
				return false;
			}
			else
			{
				regex = new RegExp(regex,"g");
				if(!regex.test(value))
				{
					//validateCfgErrorMsg="填写格式不对" + regex + "";
					validateCfgErrorMsg="填写格式不对";
					return false;
				}
			}
		}
		else if(validateType=="unique")
		{
			var valid=true;
			$(':input[validateType="unique"][disabled!=true]').each(function(){
				if(!$(this).attr("id"))
				{
					valid=false;
					validateCfgErrorMsg="标签必须带ID属性,否则无法验证";
					return false;
				}
	
				if($(this).attr("id")!=obj.attr("id")&&$(this).val()==value)
				{
					valid=false;
					return false;
				}
			});
			return valid;
		}
	}
	return true;
}

var validateMessageDivIndex=0;
function showMsg(obj,msg)
{
	if(validateCfgErrorMsg)
	{
		//优先显示validatePM的信息，使用于表单内隐藏域，如：FlwUI_NodeEntry.jsp
		msg=obj.attr("validatePM");
		if(!msg){
			msg=validateCfgErrorMsg;
		}
	}
	else if(!msg)
	{
		msg=obj.attr("validateMessage");
		if(!msg)
		{
			if(obj.attr("validateType").indexOf("required") > -1)
			{
				msg="不能为空";
			}
			else if(obj.attr("validateType").indexOf("unique") > -1)
			{
				msg="不能重复";
			}
			else if(obj.attr("validateType").indexOf("length") > -1)
			{
				msg="长度不对";
			}
			else
			{
				msg="标签没有验证失败信息:validateMessage";
			}
		}
	}
	
	/*msg="<div style='position: absolute; padding: 10px; left: 5px; font-size: 1.0em; background-color: white; border: 1px solid #a6c9e2;color:#FF0000; -moz-border-radius: 5px; -webkit-border-radius: 5px; z-index: 9999;' id='validateMessageDiv"+validateMessageDivIndex+"'><img style='position: absolute; top: -10px; left: 5px' src='/js/common/validate/vtip_arrow.png'/>" + msg + "</div>";
	var msgDiv=$(document.body).append(msg).find("#validateMessageDiv"+validateMessageDivIndex);
	msgDiv.offset({top:obj.offset().top+$(document.body).scrollTop()+obj.outerHeight(),left:obj.offset().left});
	msgDiv.focus();
	obj.bind("focus", function(){
		msgDiv.remove();
	});
	setTimeout(function(){msgDiv.remove();},3000);
	validateMessageDivIndex++;*/
	
	if(obj.attr("type") != "hidden"){
		//alert(obj.attr("name") + "===" + obj.attr("length"));
		obj.focus();
		obj.select();
	}
	alert(msg);
	return false;
}

//得到字符串的字节数，如：getByte("test测试")返回8
function getByte(s)
{
    var intCount = 0;
    for(var i = 0;i < s.length;i ++)
    {
        // Ascii码大于255是双字节的字符
        if(s.charCodeAt(i) > 255)intCount += 2;
        else intCount += 1;
    }
    return intCount;
}